using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class manager_news_view_news : System.Web.UI.Page
{
    private string id = "";
    string user_present = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        // test user login yet
        user_present = (string)Session["user"];
        if (user_present == null)
        {
            Response.Redirect("../main/login.aspx");
        }
        id = Request["id"];
        if (Page.IsPostBack == false)
        {
            if (id == "new")
            {
                btnUpdate.ImageUrl = "../images/create.gif";
            }
            else
            {                    
                 btnUpdate.ImageUrl = "../images/update.gif";
                 BindData();
            }

            get_image_on_local2(listbox_image, General.news_image_path, "*.*");
            load_filetype(ddl_type);
        }

    }

    protected void BindData()
    {
        News news = News.get_news(Convert.ToInt32(id));
        txtTITLE.Text = news.NewsTitle;
        fckSHORT.Value = news.NewsShort;
        fckDETAIL.Value = news.NewsDetail;
        image_preview.ImageUrl =AppDomain.CurrentDomain.SetupInformation.ApplicationBase + General.news_image_path + news.Images;
    }

    protected void execute(object sender, EventArgs e)
    {
        if (id == "new")
        {
            try
            {
               News.create_news(txtTITLE.Text,fckSHORT.Value,fckDETAIL.Value,Convert.ToInt32(ddl_type.SelectedValue),listbox_image.SelectedValue ,true, user_present);
                Response.Redirect("list_news.aspx");
            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(GetType(), "Message", "<script language='javascript'>alert('" + ex.Message.ToString().Replace("'", "") + "');</script>");
            }
        }
        else
        {
            try
            {
                News.update_news(Convert.ToInt32(id), txtTITLE.Text, fckSHORT.Value, fckDETAIL.Value, Convert.ToInt32(ddl_type.SelectedValue), listbox_image.SelectedValue, true, user_present);
                Response.Redirect("list_news.aspx");
            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(GetType(), "Message", "<script language='javascript'>alert('" + ex.Message.ToString().Replace("'", "") + "');</script>");
            }
        }
    }

    protected void load_filetype(DropDownList ddl_filetype)
    {
        ddl_filetype.DataSource = FileType.get_all_filetype();
        ddl_filetype.DataTextField = "Caption";
        ddl_filetype.DataValueField = "FileTypeID";
        ddl_filetype.DataBind();
    }

    protected void get_image_on_local2(ListBox listbox, string path_end, string file_type_expression)
    {
        try
        {
            string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(path + path_end);
            System.IO.FileInfo[] fileInfos = di.GetFiles(file_type_expression);

            listbox.DataSource = fileInfos;
            listbox.DataBind();
        }
        catch (Exception ex)
        {
            return;
        }
    }

    protected void listbox_image_SelectedIndexChanged(object sender, EventArgs e)
    {
        image_preview.ImageUrl = "../../" + General.news_image_path +  listbox_image.SelectedValue;
    }
}
